package org.linitly;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: linxiunan
 * @date: 2021/5/27 15:50
 * @descrption:
 */
public class Main {

    private static final String PACKAGE_NAME = "-packagename";
    private static final String PROJECT_NAME = "-projectname";
//    private static final String PROJECT_PATH = "-path";
//    private static final String BEFORE_PACKAGE_NAME = "-beforepackagename";

    private static final String CREATE_JAVA_PROJECT = "-createjava";
    private static final String CREATE_WEB_VUE_PROJECT = "-createvue";
    private static final String CREATE_WEB_REACT_PROJECT = "-createreact";
    private static final String CREATE_ALL = "-createall";

    private static final String DEFAULT_PROJECT_NAME = "linitly-boot";
    private static final String BACK_UP_DIR_NAME = "linitly-temp";


    public static void main(String[] args) throws Exception {
        boolean trueArgs = checkArgs(args);
        if (!trueArgs) return;

        Map<String, String> arguments = parseCommandLine(args);

        String projectName = arguments.get(PROJECT_NAME);
        if (StringUtils.isBlank(projectName)) {
            System.out.println("project name not incoming");
            System.exit(-1);
            return;
        }

        String packageName = arguments.get(PACKAGE_NAME);
        if (StringUtils.isBlank(packageName)) {
            System.out.println("package name not incoming");
            System.exit(-1);
            return;
        }

        String projectPath = null;
//        boolean incomePath = false;
//        if (arguments.containsKey(PROJECT_PATH)) {
//            projectPath = arguments.get(PROJECT_PATH);
//            incomePath = true;
//        } else {
//            String localPath = System.getProperty("user.dir");
//            File file = new File(localPath);
//            projectPath = file.getParentFile().getParent();
//        }
        String localPath = System.getProperty("user.dir");
        File file = new File(localPath);
        projectPath = file.getParentFile().getParent();
//System.out.println("project path:" + projectPath);
//        String beforePackageName = (incomePath && arguments.containsKey(BEFORE_PACKAGE_NAME)) ? arguments.get(BEFORE_PACKAGE_NAME) : DEFAULT_PACKAGE_NAME;

        File projectPathFile = checkAndGetProjectPathFile(projectPath, false);

        String newFilePath = projectPath.replaceFirst(projectPathFile.getName(), projectName);
        File newFile = new File(newFilePath);
        // 对应的文件夹如果已经存在，提示错误信息
        if (newFile.exists() && newFile.isDirectory()) {
            System.out.println("directory is already exists");
            System.exit(0);
        }
        // 创建项目文件夹
        boolean mkdirSuccess = newFile.mkdir();
        if (!mkdirSuccess) {
            System.out.println("create project dir error");
            System.exit(0);
        }

        boolean createAll = arguments.containsKey(CREATE_ALL);
        boolean createJava = (createAll || arguments.containsKey(CREATE_JAVA_PROJECT));
        boolean createVue = createAll || arguments.containsKey(CREATE_WEB_VUE_PROJECT);
        boolean createReact = createAll || arguments.containsKey(CREATE_WEB_REACT_PROJECT);

        FileCreate fileModify = new FileCreate(createJava, createVue, createReact);

        fileModify.create(projectName, packageName, projectPathFile, newFile);

        System.out.println("create project success");
        System.exit(0);
    }

    /**
     * @author Linitly
     * @date 15:40 2022/3/10
     * @description 期望是用户将该工具放在对应项目的tools文件夹下，所以读取的是爷爷级目录，检查该目录的正确性
     */
    private static File checkAndGetProjectPathFile(String projectPath, boolean incomePath) {
        // 判断文件路径是否正确
        File projectPathFile = new File(projectPath);
        if (!projectPathFile.exists() || !projectPathFile.isDirectory()) {
            System.out.println("project path error");
            System.exit(0);
            return null;
        }
        // 如果不是输入的路径，且路径名不是默认的项目名结尾，返回错误信息
        if (!incomePath && !projectPath.endsWith(DEFAULT_PROJECT_NAME)) {
            System.out.println("default project path error, please use the project named linitly-boot");
            System.exit(0);
            return null;
        }
        return projectPathFile;
    }

    private static boolean checkArgs(String[] args) {
        // 需要参数：1.包名  2.项目名
        if (args.length == 0) {
            System.out.println("param not incoming");
            System.exit(0);
            return false;
        }
        if (args.length < 2 * 2) {
            System.out.println("param incoming error");
            System.exit(0);
            return false;
        }
        return true;
    }

    private static Map<String, String> parseCommandLine(String[] args) {
        Map<String, String> arguments = new HashMap<String, String>();

        for (int i = 0; i < args.length; i++) {
            if (args[i].equalsIgnoreCase(PACKAGE_NAME)) {
                arguments.put(PACKAGE_NAME, args[i + 1]);
            } else if (args[i].equalsIgnoreCase(PROJECT_NAME)) {
                arguments.put(PROJECT_NAME, args[i + 1]);
            }
//            else if (args[i].equalsIgnoreCase(PROJECT_PATH)) {
//                if (i + 1 > args.length) {
//                    System.out.println("param incoming error, project path param missing");
//                    System.exit(0);
//                    return null;
//                } else {
//                    arguments.put(PROJECT_PATH, args[i + 1]);
//                }
//            } else if (args[i].equalsIgnoreCase(BEFORE_PACKAGE_NAME)) {
//                if (i + 1 > args.length) {
//                    System.out.println("param incoming error, before package name param missing");
//                    System.exit(0);
//                    return null;
//                } else {
//                    arguments.put(BEFORE_PACKAGE_NAME, args[i + 1]);
//                }
//            }
            else if (args[i].equalsIgnoreCase(CREATE_JAVA_PROJECT)) {
                arguments.put(CREATE_JAVA_PROJECT, "Y");
            } else if (args[i].equalsIgnoreCase(CREATE_WEB_VUE_PROJECT)) {
                arguments.put(CREATE_WEB_VUE_PROJECT, "Y");
            } else if (args[i].equalsIgnoreCase(CREATE_WEB_REACT_PROJECT)) {
                arguments.put(CREATE_WEB_REACT_PROJECT, "Y");
            } else if (args[i].equalsIgnoreCase(CREATE_ALL)) {
                arguments.put(CREATE_ALL, "Y");
            }
        }

        if (arguments.isEmpty()) {
            System.out.println("param incoming error");
            System.exit(-1);
        }
        return arguments;
    }
}
